package com.tentact.web1216.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Dbutil {
	private static final String url = "jdbc:mysql://127.0.0.1:3306/mysql?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 ";
	private static final String uesrName = "root";
	private static final String password = "root";
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	// ��ȡ���ӵķ���
	public static Connection getconnection() {
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, uesrName, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

	// �ر���Դ�ķ���
	public static void close(Connection conn) {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(Connection conn, PreparedStatement psmt) {
		try {
			psmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(Connection conn, PreparedStatement psmt, ResultSet rs) {
		try {
			rs.close();
			psmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	//��ɾ��ͨ�ù��߷���
	public static int executUdate(String sql ,Object [] paras ) {
		int result = -1;
		Connection conn=getconnection();
		PreparedStatement psmt=null ;
		try {
			psmt=conn.prepareStatement(sql);
			if (paras!=null) {
				for(int i=0 ; i<paras.length ; i++) {
					psmt.setObject(i+1, paras[i]);
				}
			}
			result =psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close(conn,psmt);
		}
		return result ;
		
	}
	
	//��ѯ��ͨ�÷���������ֵΪ���󼯺ϣ�
	public static <T> List<T> executeQuery(String sql,Object[] paras,RowMapper<T> mapper){
		Connection conn=getconnection();
		PreparedStatement psmt=null;
		ResultSet rs=null;
		List<T> list =new ArrayList<T>();
		try {
			psmt=conn.prepareStatement(sql);
			if (paras !=null) {
				for(int i=0;i<paras.length;i++) {
					psmt.setObject(i+1, paras[i]);
				}
			}
			rs=psmt.executeQuery();
			while (rs.next()) {
				//��ȡrs�ﵱǰ�������ݵĸ�����ֵ�������ɶ��󣬴��뵽����
				T object=mapper.rowMapper(rs);
				list.add(object);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close(conn, psmt, rs);
		}
		return list;
	}
	//��ѯ��ͨ�÷��� ������ֵΪһ������
	public static <T> T executeQuerySingle(String sql,Object[] paras,RowMapper<T> mapper) {
		T object=null; 
		PreparedStatement psmt=null;
		ResultSet rs=null;
		Connection conn= getconnection();
		try {
			psmt =conn.prepareStatement(sql);
			if (paras!=null) {
				for(int i=0 ; i<paras.length ; i++) {
					psmt.setObject(i+1, paras[i]);
				}
			}
			rs=psmt.executeQuery();
			if (rs.next()) {
				object=mapper.rowMapper(rs);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close(conn,psmt,rs);
		}
		return object ;
	}
}
